<template>
    <div>
        <el-button type="text" @click="saveUI">添加药品</el-button>
        <el-dialog title="" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="药品名称" :label-width="formLabelWidth">
                    <el-input v-model="form.medicineName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="药品价格" :label-width="formLabelWidth">
                    <el-input v-model="form.medicinePrice" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="药品数量" :label-width="formLabelWidth">
                    <el-input v-model="form.medicineNum" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="描述" :label-width="formLabelWidth">
                    <el-input v-model="form.medicineDescription" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="药物预防" :label-width="formLabelWidth">
                    <el-input v-model="form.medicinePrecautions" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitform">确 定</el-button>
            </div>
        </el-dialog>

        <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column prop="medicineId" label="编号" width="100">
            </el-table-column>
            <el-table-column prop="medicineName" label="药品名" width="180">
            </el-table-column>
            <el-table-column prop="medicinePrice" label="药品价格" width="100">
            </el-table-column>
            <el-table-column prop="medicineNum" label="药品数量" width="180">
            </el-table-column>
            <el-table-column prop="medicineDescription" label="描述" width="180">
            </el-table-column>
            <el-table-column prop="medicinePrecautions" label="药物预防" width="180">
            </el-table-column>
            <el-table-column prop="status" label="状态" width="180">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.status" :active-value="0" :inactive-value="1"
                        @change="changeStatus(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="180">
                <template slot-scope="scope">
                    <el-button type="danger" @click="del(scope.row)">删除</el-button>
                    <el-button type="info" @click="updateUI(scope.row)">修改</el-button>
                </template>

            </el-table-column>
        </el-table>

        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
            :page-sizes="[5, 10, 15, 20]" :page-size="100" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
    </div>
</template>
  
<script>
import axios from '@/util/request.js';

export default {
    data() {
        return {
            searchResults: [],
            name: "",
            dialogFormVisible: false,
            form: {
            },
            formInline: {
                deptName: "",
                name: ""
            },
            total: 0,
            formLabelWidth: '120px',
            tableData: [],
            pageNum: 1,
            pageSize: 5
        }
    },

    methods: {
        changeStatus(row) {
            let medicineId = row.medicineId
            axios.post("medicine/changStatus", medicineId, axios.textConfig).then(r => {
                if (r.code == 200) {
                    this.getList()
                }
            })
        },
        handleSizeChange(size) {
            this.pageSize = size;
            this.getList();
        },
        handleCurrentChange(page) {
            this.pageNum = page;
            this.getList();
        },
        saveUI() {
            this.form = {}
            this.dialogFormVisible = true
        },

        submitform() {
            let path = !this.form.medicineId ? "save" : "update";
            axios.post(`medicine/${path}`, this.form).then(r => {
                if (r.code == 200) {
                    this.dialogFormVisible = false
                    this.getList();
                    this.form = {};
                    this.$message.success(r.msg)
                    return;
                }
                this.$message.error(r.msg)
            })

        },
        getList() {
            axios.post(`medicine/findSomeByPage/${this.pageNum}/${this.pageSize}`)
                .then(r => {
                    this.tableData = r.data.list
                    this.total = r.data.total
                })
        },
        del(row) {
            if (window.confirm("确认删除吗？")) {
                let medicineId = row.medicineId
                axios.post("medicine/delete", medicineId, axios.textConfig).then(r => {
                    if (r.code == 200) {
                        this.getList();
                        this.$message.success("删除成功")
                    } else {
                        this.$message.error("删除失败")
                    }
                })
            }
        },
        updateUI(row) {
            let str = JSON.stringify(row)
            let json = JSON.parse(str)
            this.form = json
            this.dialogFormVisible = true
        },
        distributeRoleUI(row) {
            this.$router.push(`/Home/DistributeRole/${row.medicineId}`)
        }

    },
    mounted() {
        this.getList()
    }
}
</script>